import { ref, computed, reactive } from 'vue'
import { defineStore } from 'pinia'


export const useAdminStore = defineStore(
    //id
    'admin',
    //定义初始值
    () => {
        // 定义token
        const token = ref('')
      
        //   获取token
        const getToken = computed(() => token.value)
        //   保存token
        const saveToken = (t) => {
            token.value = t


        }

        const admin =reactive({
            username:'',
            realName:'',
            avatar:'',
            adminId:0
        })
        //保存用户信息
        const saveAdmin =(myAdmin)=>{
            admin.username=myAdmin.username
            admin.realName=myAdmin.realName
            admin.avatar=myAdmin.imagePath
            admin.adminId=myAdmin.adminId

        }
     
        // 要返回token， 因为要持久化token
        return { getToken, saveToken,token,admin,saveAdmin }
    },
    //持久化
    {
        persist: {
            //开启持久化
            enabled: true,
            //策略
            strategies: [
                {


                    //持久化key名称
                    key: 'admin-info',
                    //存储方式 localstorage  和sessionstorage
                    //localstorage永久本地保存，sessionstorage存储至页面关闭
                    storage: localStorage,
                    //指定state中那些数据需要被持久化
                    //paths是空数组表示表示不持久化任何状态
                    //paths为undefined或者null表示持久化整个state
                    //paths: ['token']
                }]
        }


    }
)
